/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package indexList;

/**
 *
 * @author pc
 */
public class ArrayIndexList<E> implements IndexList<E> {

    private E[] IL;
    private final int CAPACITY = 1024;
    private int cap, size;

    public ArrayIndexList() {
        IL = (E[]) new Object[CAPACITY];
        cap = CAPACITY;
        size = 0;
    }

    public ArrayIndexList(int cap) {
        this.cap = cap;
        IL = (E[]) new Object[cap];
        size = 0;
    }

    @Override
    public E remove(int i) throws IndexOutOfBoundsException {
        E tmp = IL[i];
        for (int j = i; j < size(); j++) {
            IL[j] = IL[j++];
        }
        size--;
        return tmp;
    }

    @Override
    public void add(int i, E e) throws IndexOutOfBoundsException {
     
        if (i+1 >= cap) {
            E[] newArray = (E[]) new Object[cap * 2];
            for (int x = 0; x < size(); x++) {
                newArray[x] = IL[x];
            }

            cap *= 2;
            IL = newArray;
        }
       
        System.out.println("Allert:"+i+"  "+size()+"  "+cap);
        
        size++;
        for (int j = i; j < size(); j++) {
            IL[j + 1] = IL[j];
        }
        IL[i] = e;
        
      

    }

    @Override
    public E set(int i, E e) throws IndexOutOfBoundsException {
        if (i > size()) {
            throw new IndexOutOfBoundsException();
        }
        E tmp= IL[i];
        IL[i]=e;
        return tmp;
    }

    @Override
    public E get(int i) throws IndexOutOfBoundsException {
        if (i > size()) {
            throw new IndexOutOfBoundsException();
        }
        return IL[i];
    }

    @Override
    public boolean isEmpty() {
        return size() == 0 ? true : false;
    }

    @Override
    public int size() {
        return size;
    }
}
